package com.txby.wrApi.component.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageBean;
import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.wrApi.data.CommonApiModelMapExt;
import com.txby.wrApi.data.CommonMybaitsDataMapExt;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 进度查看数据相关
 * 
 * @ClassName: Gzz01ApiRawDataController
 * @Description: TODO （描述）
 * @author 作者 jacking
 * @date 2017年6月23日 上午9:32:34
 */
@SuppressWarnings("all")
@RestController
@Api(value = "进度查看数据相关", description = "进度查看数据相关")
@RequestMapping("/gzz01/api")
public class ApiDailyProgressDataController extends CommonController {
	private static Logger log = Logger.getLogger(ApiDailyProgressDataController.class);

	/**
	 * 进度查看数据查询
	 * 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 1 reginDeviceIDs 获取的设备编号sn，多个“，”分隔
	 *               String 是 2 deviceID 筛选的设备编号sn，单个 String 2 dataType
	 *               筛选的数据类型（如：alarm,cal） String 3 checkStartTime
	 *               筛选的检测开始时间(yyyy-MM-dd) String 4 checkEndTime
	 *               筛选的检测结束时间(yyyy-MM-dd) String 5 projectName 项目名称 String 6
	 *               curPage 当前页，默认1 String 7 pageSize 页大小，默认10 String
	 * @return
	 */
	@ApiOperation(value = "进度查看数据查询", notes = "根据参数查询项目进度数据")
	@PostMapping(value = "/dailyProgressData/dailyProgressDataByApi/{type}/{token}")
	public Map dailyProgressDataByApi(@PathVariable("type") String type, @PathVariable("token") String token,
			String reginDeviceIDs,String curPage, String pageSize,String projectName,String companyName,String projectStatus,String manageName
			,String order,String sortField) {
		int currPage=1;
		if(!ChkUtil.isEmpty(curPage)){
			currPage=Integer.parseInt(curPage);
		}
		int pageSize1=10;
		if(!ChkUtil.isEmpty(pageSize)){
			pageSize1=Integer.parseInt(pageSize);
		}
		CommonApiModelMapExt mm = chekTokenCahceValid(token, type);
		if (!isTokenValid) {
			return mm;
		}
		// 查询数据
		CommonMybaitsDataMapExt cmm = getSplitInSqlByMapData(reginDeviceIDs, "cp.deviceID",null);
		if(cmm==null){
			mm.put("curPage", 1);
			mm.put("pageSize", 10);
			mm.put("totalCount", 0);
			mm.put("totalPage", 0);
			mm.put("data", new ArrayList<>());
			mm.put("msg", "无数据，确认所查sn编号是否正确");
			mm.put("info", "无数据，确认所查sn编号是否正确");
			return mm;
		}
		Map paramMap = new HashMap<>();
		String searchSql = "";
		String sortStr = "";
		int orderBy=0;
		if(!ChkUtil.isEmpty(order)&&StringUtils.isNumeric(order)){
			orderBy=Integer.parseInt(order);
		}
		if(!StringUtils.isNumeric(projectStatus)){
			projectStatus=null;
		}
	 
	    
		if (!ChkUtil.isEmpty(projectName)) {
			if (projectName.contains("%") || projectName.contains("_")) {
				projectName = projectName.trim().replace("%", "\\%").replace("_", "\\_");
				projectName = "%" + projectName + "%";
				searchSql += " and p.name like #{pojo.projectName} escape '\\'";
				paramMap.put("projectName", "%" + projectName.trim() + "%");
			} else {
				projectName = "%" + projectName.trim() + "%";
				searchSql += " and p.name like #{pojo.projectName} ";
				paramMap.put("projectName", "%" + projectName.trim() + "%");
			}
		}
		if (!ChkUtil.isEmpty(companyName)) {
			if (companyName.contains("%") || companyName.contains("_")) {
				companyName = companyName.trim().replace("%", "\\%").replace("_", "\\_");
				companyName = "%" + companyName + "%";
				searchSql += " and c.name like #{pojo.companyName} escape '\\'";
				paramMap.put("companyName", "%" + companyName.trim() + "%");
			} else {
				companyName = "%" + companyName.trim() + "%";
				searchSql += " and c.name like #{pojo.companyName} ";
				paramMap.put("companyName", "%" + companyName.trim() + "%");
			}
		}
		if (!ChkUtil.isEmpty(manageName)) {
			if (manageName.contains("%") || manageName.contains("_")) {
				manageName = manageName.trim().replace("%", "\\%").replace("_", "\\_");
				manageName = "%" + manageName + "%";
				searchSql += " and u.userName like #{pojo.manageName} escape '\\'";
				paramMap.put("manageName", "%" + manageName.trim() + "%");
			} else {
				manageName = "%" + manageName.trim() + "%";
				searchSql += " and u.userName like #{pojo.manageName} ";
				paramMap.put("manageName", "%" + manageName.trim() + "%");
			}
		}
		
		if (!ChkUtil.isEmpty(projectStatus)) {
			searchSql += " and p.status=#{pojo.projectStatus} ";
			paramMap.put("projectStatus", projectStatus.trim());
		}

		 

//		searchSql += cmm.getInSqlByAnd();
		paramMap.putAll(cmm.getPojoMap());
		
		if(!ChkUtil.isEmpty(sortField)){
			switch (sortField) {
			case "projectName":
				sortStr="  p.name ";
				sortStr+=orderBy<0?" desc ":"";
				break;
			case "pileTotalNum":
				sortStr="  p.pileTotalNum ";
				sortStr+=orderBy<0?" desc ":"";
				break;
			case "checkFinishNum":
				sortStr="  p.checkFinishNum ";
				sortStr+=orderBy<0?" desc ":"";
				break;
			case "noFinishNum":
				sortStr="  p.noFinishNum ";
				sortStr+=orderBy<0?" desc ":"";
				break;
			case "otherFinishNum":
				sortStr="  p.otherFinishNum ";
				sortStr+=orderBy<0?" desc ":"";
				break;
			default:
				sortStr="  p.name ";
				break;
			}
		 
		}else{
			sortStr="  p.name ";
		}
		String gzzDeviceIdSql=" INNER JOIN (select DISTINCT cp.constructProjectId from  t_biz_gzz01CProject_Device cp "+cmm.getInSqlByWhere()+") cpd on cpd.constructProjectId=p.id ";
		String sql = "SELECT row_number () OVER (ORDER BY " + sortStr + ",p.status ASC) AS rownumber, "
				+ " p.name as projectName, p.status as projectStatus, p.pileTotalNum as pileTotalNum, p.checkFinishNum as checkFinishNum, p.otherFinishNum as otherFinishNum, p.noFinishNum as noFinishNum, u.userName as manageName,c.name AS companyName "
				+ " FROM t_bd_gzz01ConstructProject p INNER JOIN t_bd_gzz01User u ON p.manageId = u.id INNER JOIN t_bd_gzz01Company c ON p.companyId = c.id " +gzzDeviceIdSql
				+ " WHERE 1 = 1 ";
		sql += searchSql;
		
		sql="select t.projectName,t.projectStatus,t.pileTotalNum,t.checkFinishNum,t.otherFinishNum,t.noFinishNum,t.manageName,t.companyName from ("+sql+") t";
		PageBean pageBean = new PageBean(currPage, pageSize1);
		PageList pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, pageBean, paramMap);
		if(!ChkUtil.isEmpty(tempTableName)){
			this.mBaseDao.executeSql("drop table "+tempTableName+"", null);
			this.tempTableName="";
		}
		mm.put("curPage", pl.getPageBean().getPage());
		mm.put("pageSize", pl.getPageBean().getPageSize());
		mm.put("totalCount", pl.getPageBean().getTotalCount());
		mm.put("totalPage", pl.getPageBean().getTotalPage());
		mm.put("data", pl.getResultList());
		if(pl.getPageBean().getTotalCount()==0){

			/*mm.put("msg", "无数据，请确认所查sn编号是否正确");
			mm.put("info", "无数据，请确认所查sn编号是否正确");*/
			mm.put("msg", "无数据");
			mm.put("info", "无数据");

			mm.put("msg", "无数据");
			mm.put("info", "无数据");

		}
		return mm;
	}
 
	

}